package com.harkjeans.javacoding.basic;

/**
 * ClassName: Exercise3
 * Package: com.harkjeans.javacoding.basic
 * Description: 练习3:验证码
 *
 * @Author harkjeans
 * @Create 2025/10/14 13:51
 * @Version 1.0
 */

import java.util.Random;

/**
 * 定义方法实现随机产生一个5位的验证码
 * 验证码格式：
 * 长度为5、前四位是大写字母或者小写字母、最后一位是数字
 */
public class Exercise3 {
    public static void main(String[] args) {
        // 分析
        // 将52个字母(由ASCII表映射实现)放入数组中,然后通过随机索引来实现
        char[] chs = new char[52];
        for (int i = 0; i < chs.length; i++) {
            if (i < 26) {
                chs[i] = (char) (97 + i);
            } else {
                chs[i] = (char) (65 + i - 26);
            }
        }

        String result = "";

        Random r = new Random();

        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(chs.length);
            result = result + chs[randomIndex];
        }

        int num = r.nextInt(10);
        result = result + num;
        System.out.println(result);


    }
}
